package com.yuanqi.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuanqi.Common.Pojo.entity.Blacklist;
import com.yuanqi.Common.config.ThreadLocalConfig;
import com.yuanqi.Return.Result;
import com.yuanqi.Service.BlackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;

import javax.annotation.Resource;

@RestController
@RequestMapping("/v1/black")
public class BlackController {

    @Resource
    BlackService blackService;
    @Resource
    ThreadLocalConfig threadLocalConfig;
    /**
     * 添加黑名单
     * @return
     */
    @PostMapping("/addBlack")
    public Result addBlack(@RequestBody Blacklist blacklist){
        Long userId = Long.valueOf(threadLocalConfig.get().get("userId").toString());
        blacklist.setCreateTime(System.currentTimeMillis()/1000);
        blacklist.setUserId(userId);
        blackService.save(blacklist);
        return Result.success("添加成功");
    }
    /**
     * 删除黑名单
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestBody Blacklist blacklist){
        Long userId = Long.valueOf(threadLocalConfig.get().get("userId").toString());
        LambdaQueryWrapper<Blacklist> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Blacklist::getBlackId,blacklist.getBlackId());
        wrapper.eq(Blacklist::getUserId,userId);
        blackService.remove(wrapper);
        return Result.success();
    }
}
